/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package arkanoid;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import p3as.cliente.Ranking;

class DBScore implements ScoreManager{

    @Override
    public void saveScore(String user, int score) {
        try {
            FileOutputStream file = new FileOutputStream("C:/Documents and Settings/Josue/Mis documentos/score" + user +".txt");
            ObjectOutputStream ostream = new ObjectOutputStream(file);
            ostream.writeInt(score);
            //ostream.writeChars(user);
            ostream.flush();
            ostream.close();
        } catch (Exception ex) {
            System.out.println(ex.getCause());
        }
    }

    @Override
    public Ranking loadRanking(String user) {
        try {
            FileInputStream fileInputStream = new FileInputStream("C:/Documents and Settings/Josue/Mis documentos/score" + user +".txt");
            DataInputStream inputStream = new DataInputStream(fileInputStream);
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
            Ranking ranking = new Ranking();
            String linea = in.readLine();
            while(linea != null){
                ranking.addScore(Integer.valueOf(linea));
                linea = in.readLine();
            }
            return ranking;
        } catch (Exception ex) {
            Logger.getLogger(DBScore.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
}
